package com.itmk.student_point.dao.impl;

import com.itmk.common.BaseDao;
import com.itmk.student_point.dao.StudentPointDao;
import com.itmk.student_point.entity.StudentPoint;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class StudentPointDaoImpl extends BaseDao implements StudentPointDao {
    @Override
    public void add(StudentPoint studentPoint) {
        //1、定义sql
        String sql = "insert into student_point(stu_id,course_id,points) values(?,?,?)";
        //2、定义handler
        BeanHandler<StudentPoint> handler = new BeanHandler<>(StudentPoint.class);
        //3、参数封装
        Object[] parms = {
                studentPoint.getStuId(),
                studentPoint.getCourseId(),
                studentPoint.getPoints()
        };
        try {
            query.insert(sql,handler,parms);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public StudentPoint getById(StudentPoint studentPoint) {
        //1、定义sql
        String sql = "select * from student_point where stu_id=? and course_id=?";
        //2、定义handler
        BeanHandler<StudentPoint> handler = new BeanHandler<>(StudentPoint.class,processor);
        //3、参数封装
        Object[] parms = {
                studentPoint.getStuId(),
                studentPoint.getCourseId()
        };
        try {
            return query.query(sql,handler,parms);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteById(int Id) {
        //1、定义SQL
        String sql = "delete from student_point where point_id=?";
        try {
            query.update(sql,Id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
